package com.example.app;

import java.util.Map;
import java.util.WeakHashMap;

import net.sf.calibur.msgbuf.CommandFactory;
import net.sf.calibur.engine.CaliburEngine;


public class AppContext {
	public static String ATTR_PLAYER = "player_instance";
	
	static class SingletonHolder {
		static AppContext singleton = new AppContext();
	}
	
	public static AppContext getInstance() {
		return SingletonHolder.singleton;
	}
	
	private CommandFactory messageBeanFactory;
	private CaliburEngine engine;
	private Map<Thread, HandlerContext> handlerContexts;
	
	private AppContext() {
		handlerContexts = new WeakHashMap<Thread, HandlerContext>();
	}

	public CommandFactory getMessageBeanFactory() {
		return messageBeanFactory;
	}

	public void setMessageBeanFactory(CommandFactory messageBeanFactory) {
		this.messageBeanFactory = messageBeanFactory;
	}
	
	public CaliburEngine getEngine() {
		return engine;
	}
	
	public void setEngine(CaliburEngine engine) {
		this.engine = engine;
	}
	
	public synchronized HandlerContext getHandlerContext() {
		HandlerContext hctx = handlerContexts.get(Thread.currentThread());
		if (hctx == null) {
			hctx = new HandlerContext();
			handlerContexts.put(Thread.currentThread(), hctx);
		}
		
		return hctx;
	}
}
